<?php

namespace App\Http\Controllers\App;

use App\Helpers\Common;
use App\Helpers\Wx;
use App\Http\Controllers\Controller;
use App\Http\Requests\AddressRequest;
use App\Models\Address;
use App\Models\Cart;
use App\Models\Merchant;
use App\Models\SUsers;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * 小程序登录
     * @param Request $request
     * @return array|mixed
     * @throws \Throwable
     */
    public function login(Request $request)
    {
        $code = $request->get('code');
        $wx = new Wx();
        $result = $wx->code2Session($code);

        if(!isset($result['openid'])){
            return[
                "result" => 'failed',
                "message" => $result
            ];
        }

        $user = SUsers::where('openid',$result['openid'])->first();
        if (!$user){
            $user = new SUsers();
            $user->openid = $result['openid'];
            $user->session_key = $result['session_key'];
            $user['nickname'] = '用户' . rand(1000, 9999);
            $user->saveOrFail();
            return [
                "result" => "success",
                "data" => [
                    'openid'=>$result['openid'],
                    'cart_amount'=>0,
                    'is_merchant'=>null,
                    'address'=>null,
                ]
            ];
        }
        $user->session_key = $result['session_key'];
        $user->saveOrFail();
        $merchant = Merchant::where('user_id',$user->id)->first();
        $cart_amount = Cart::where('user_id',$user->id)->count();
        $address = Address::where('user_id',$user->id)->where('default',1)->first();
        return [
            "result" => "success",
            "data" =>[
                'openid'=>$result['openid'],
                'cart_amount'=>$cart_amount,
                'is_merchant'=>$merchant,
                'address'=>$address,
                'userInfo' => $user,
            ]
        ];
    }

    public function getUser(Request $request)
    {
        return [
            'result' => 'success',
            'data' => getUser($request)
        ];
    }

    public function updateUser(Request $request)
    {
        $user = getUser($request);
        $nickname = $request->get('nickname', '请更新昵称');
        $avatar = $request->get('avatar');
        $gender = $request->get('gender');
        $country = $request->get('country');
        $province = $request->get('province');
        $city = $request->get('city');
        $user['nickname'] = $nickname;
        $user['avatar'] = $avatar;
        $user['gender'] = $gender;
        $user['country'] = $country;
        $user['province'] = $province;
        $user['city'] = $city;
        $user->save();
        return [
            'result' => 'success',
            'data' => $user
        ];
    }


    public function addAddress(AddressRequest $request){
        $user = getUser($request);
        $request_address = $request->get('address');
        $address = Address::where('user_id',$user->id)->first();
        if ($address){
            $address['province'] = $request_address['province'];
            $address['city'] = $request_address['city'];
            $address['district'] = $request_address['district'];
            $address['town'] = $request_address['town'];
            $address['default'] = 1;
            $address->saveOrFail();
        }else{
            $address = new Address;
            $address['user_id'] = $user->id;
            $address['province'] = $request_address['province'];
            $address['city'] = $request_address['city'];
            $address['district'] = $request_address['district'];
            $address['town'] = $request_address['town'];
            $address['default'] = 1;
            $address->saveOrFail();
        }


        return[
            'result'=>'success',
            'message'=>'存储地址成功'
        ];
    }


}
